package org.jeecg.modules.yks.dev.service.impl;

import org.jeecg.modules.yks.dev.entity.YksMaintenanceTemplate;
import org.jeecg.modules.yks.dev.entity.YksMaintenanceTemplateDetail;
import org.jeecg.modules.yks.dev.mapper.YksMaintenanceTemplateDetailMapper;
import org.jeecg.modules.yks.dev.mapper.YksMaintenanceTemplateMapper;
import org.jeecg.modules.yks.dev.service.IYksMaintenanceTemplateService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 保养内容模板表
 * @Author: jeecg-boot
 * @Date:   2023-12-03
 * @Version: V1.0
 */
@Service
public class YksMaintenanceTemplateServiceImpl extends ServiceImpl<YksMaintenanceTemplateMapper, YksMaintenanceTemplate> implements IYksMaintenanceTemplateService {

	@Autowired
	private YksMaintenanceTemplateMapper yksMaintenanceTemplateMapper;
	@Autowired
	private YksMaintenanceTemplateDetailMapper yksMaintenanceTemplateDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(YksMaintenanceTemplate yksMaintenanceTemplate, List<YksMaintenanceTemplateDetail> yksMaintenanceTemplateDetailList) {
		yksMaintenanceTemplateMapper.insert(yksMaintenanceTemplate);
		if(yksMaintenanceTemplateDetailList!=null && yksMaintenanceTemplateDetailList.size()>0) {
			for(YksMaintenanceTemplateDetail entity:yksMaintenanceTemplateDetailList) {
				//外键设置
				entity.setTemplateId(yksMaintenanceTemplate.getId());
				yksMaintenanceTemplateDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(YksMaintenanceTemplate yksMaintenanceTemplate,List<YksMaintenanceTemplateDetail> yksMaintenanceTemplateDetailList) {
		yksMaintenanceTemplateMapper.updateById(yksMaintenanceTemplate);
		
		//1.先删除子表数据
		yksMaintenanceTemplateDetailMapper.deleteByMainId(yksMaintenanceTemplate.getId());
		
		//2.子表数据重新插入
		if(yksMaintenanceTemplateDetailList!=null && yksMaintenanceTemplateDetailList.size()>0) {
			for(YksMaintenanceTemplateDetail entity:yksMaintenanceTemplateDetailList) {
				//外键设置
				entity.setTemplateId(yksMaintenanceTemplate.getId());
				yksMaintenanceTemplateDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		yksMaintenanceTemplateDetailMapper.deleteByMainId(id);
		yksMaintenanceTemplateMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			yksMaintenanceTemplateDetailMapper.deleteByMainId(id.toString());
			yksMaintenanceTemplateMapper.deleteById(id);
		}
	}
	
}
